<template>
  <el-dialog
    :title="title"
    :width="width"
    top="30px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="isVisible"
  >
    <el-form ref="form" :model="genConfig" :rules="rules" label-position="right" label-width="100px">
      <el-form-item label="数据库名" prop="tableSchema">
        <el-select v-model="genConfig.tableSchema" placeholder="请选择" style="width:100%">
          <el-option
            v-for="(item,index) in tbaleSchemaList"
            :key="index"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="作者" prop="author">
        <el-input v-model="genConfig.author" />
      </el-form-item>

      <el-form-item label="服务名" prop="serviceName">
        <el-input v-model="genConfig.serviceName" />
      </el-form-item>

      <el-form-item label="基础包名" prop="basePackage">
        <el-input v-model="genConfig.basePackage" />
      </el-form-item>

      <el-form-item label="entity路径" prop="entityPackage">
        <el-input v-model="genConfig.entityPackage" />
      </el-form-item>

      <el-form-item label="mapper路径" prop="mapperPackage">
        <el-input v-model="genConfig.mapperPackage" />
      </el-form-item>

      <el-form-item label="xml路径" prop="mapperXmlPackage">
        <el-input v-model="genConfig.mapperXmlPackage" />
      </el-form-item>

      <el-form-item label="service路径" prop="servicePackage">
        <el-input v-model="genConfig.servicePackage" />
      </el-form-item>

      <el-form-item label="去除表前缀" prop="isTrim">
        <el-select v-model="genConfig.isTrim" :value="genConfig.isTrim" placeholder="" style="width:100%">
          <el-option value="0" label="否" />
          <el-option value="1" label="是" />
        </el-select>
      </el-form-item>

      <el-form-item label="表前缀" prop="trimValue">
        <el-input v-model="genConfig.trimValue" />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="warning" plain :loading="buttonLoading" @click="isVisible = false">
        {{ $t('common.cancel') }}
      </el-button>
      <el-button type="primary" plain :loading="buttonLoading" @click="submitForm">
        {{ $t('common.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>

export default {
  name: 'GenConfigEdit',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      tbaleSchemaList: [],
      genConfig: this.initGenConfig(),
      buttonLoading: false,
      screenWidth: 0,
      width: this.initWidth(),
      rules: {
        tableSchema: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' },
          { min: 4, max: 40, message: this.$t('rules.range4to40'), trigger: 'blur' }
        ],
        author: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' },
          { min: 3, max: 20, message: this.$t('rules.range3to20'), trigger: 'blur' }
        ],
        serviceName: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' },
          { min: 4, max: 40, message: this.$t('rules.range4to40'), trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (!this.genConfig.id) {
              this.$get(`system/generatorConfig/check/${value}`).then((r) => {
                if (r.data.status === 200) {
                  callback(this.$t('rules.genconfigExist'))
                } else {
                  callback()
                }
              })
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        basePackage: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' },
          { min: 4, max: 40, message: this.$t('rules.range4to40'), trigger: 'blur' }
        ],
        entityPackage: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' },
          { min: 4, max: 10, message: this.$t('rules.range4to10'), trigger: 'blur' }
        ],
        mapperPackage: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' },
          { min: 4, max: 10, message: this.$t('rules.range4to10'), trigger: 'blur' }
        ],
        mapperXmlPackage: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' },
          { min: 4, max: 10, message: this.$t('rules.range4to10'), trigger: 'blur' }
        ],
        servicePackage: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' },
          { min: 4, max: 10, message: this.$t('rules.range4to10'), trigger: 'blur' }
        ],
        trimValue: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' },
          { min: 2, max: 10, message: this.$t('rules.range2to10'), trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    isVisible: {
      get() {
        return this.dialogVisible
      },
      set() {
        this.close()
        this.reset()
      }
    }
  },
  mounted() {
    window.onresize = () => {
      return (() => {
        this.width = this.initWidth()
      })()
    }
    this.getTableSchemaList()
  },
  methods: {
    getTableSchemaList() {
      this.$get('system/generator/database').then((r) => {
        if (r.data.status === 200) {
          this.tbaleSchemaList = r.data.data
        }
      })
    },
    initGenConfig() {
      return {
        id: '',
        tableSchema: '',
        author: '',
        serviceName: '',
        basePackage: 'com.feng.cloud',
        entityPackage: 'entity',
        mapperPackage: 'mapper',
        mapperXmlPackage: 'mapper',
        servicePackage: 'service',
        controllerPackage: 'controller',
        isTrim: '1',
        trimValue: 't_'
      }
    },
    initWidth() {
      this.screenWidth = document.body.clientWidth
      if (this.screenWidth < 991) {
        return '90%'
      } else if (this.screenWidth < 1400) {
        return '45%'
      } else {
        return '800px'
      }
    },
    close() {
      this.$emit('close')
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          if (!this.genConfig.id) {
            // create
            this.$post('system/generatorConfig', { ...this.genConfig }).then(() => {
              this.buttonLoading = false
              this.isVisible = false
              this.$message({
                message: this.$t('tips.createSuccess'),
                type: 'success'
              })
              this.$emit('success')
            }).catch(() => {
              this.buttonLoading = false
            })
          } else {
            // update
            this.$put('system/generatorConfig', { ...this.genConfig }).then(() => {
              this.buttonLoading = false
              this.isVisible = false
              this.$message({
                message: this.$t('tips.updateSuccess'),
                type: 'success'
              })
              this.$emit('success')
            })
          }
        } else {
          return false
        }
      })
    },
    setGenconfig(row) {
      this.genConfig = { ...row }
      if (this.genConfig.isTrim === 1) {
        this.genConfig.isTrim = '1'
      } else {
        this.genConfig.isTrim = '0'
      }
    },
    reset() {
      // 先清除校验，再清除表单，不然有奇怪的bug
      this.$refs.form.clearValidate()
      this.$refs.form.resetFields()
      this.genConfig = this.initGenConfig()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
